package com.nstar.base.controls
{
	import com.nstar.base.itemrenderer.ImageItemRenderer;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.ViewStack;
	import mx.events.FlexEvent;
	/**
	 * @name 		: panorama image viewer
	 * @description : a component to view a series image,a panorama viewer.
	 * 				  you can show some images(etc. product pictures) dynamically to your customer.
	 * @date		: 2010.03
	 */ 
	public class Panorama360 extends ViewStack
	{
		private var timer:Timer;
		
		private var _dataProvider:Array;//
		
		private var _interval : int = 100;//
		
		public function Panorama360()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			this.creationPolicy = "all";
			timer = new Timer(interval);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);
		}
		
		//loop display each image
		private function timerHandler(event:TimerEvent):void{
			var idx:int = this.selectedIndex;
			idx++;
			if(idx>this.numChildren-1){
				idx = 0;
			}
			this.selectedIndex = idx;
		}
		
		public function start():void{
			if(!timer.running){
				timer.start();
			}
		}
		
		public function pause():void{
			
		}

		public function stop():void{
			timer.reset();
			timer.stop();
		}
		
		//value is a object,every object must has a source property. 
		public function set dataProvider(value:Array):void{
			this._dataProvider = value;
			if(this._dataProvider){
				for each(var item:Object in this._dataProvider){
					var imageItem:ImageItemRenderer = new ImageItemRenderer();
					//item is binded to ImageitemRender object
					imageItem.item =item;
					this.addChild(imageItem);
				}
			}
		}
		
		public function get dataProvider():Array{
			return this._dataProvider;
		}
		
		//every interval,next image will be displayed.
		public function set interval(value:int):void{
			this._interval = value;
			timer.delay = value;
		}
		
		public function get interval():int{
			return this._interval;
		}
		
		//display image step by step,step is positive or negative integer
		//default is 1
		public function stepBy( step:int = 1):void{
			var idx:int = this.selectedIndex;
			idx += step;
			if(idx>this.numChildren-1){
				idx = 0;
			}
			if(idx<0){
				idx = this.numChildren - 1;
			}
			
			this.selectedIndex = idx;
		}
		
		//prior image
		public function prior():void{
			stepBy(-1);
		}
		
		//next image
		public function next():void{
			stepBy(1);
		}
	}
}